package service;

import dao.FruitMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.Fruit;
import utils.MybatisUtils;

import java.util.List;

public class FruitService {
    //查询所有
    public List<Fruit> getFruit(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        FruitMapper FruitDao = sqlSession.getMapper(FruitMapper.class);
        List<Fruit> fruits = FruitDao.getFruit();
        sqlSession.close();
        return fruits;
    }
    //添加
    public int addFruit(Fruit fruit){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        FruitMapper fruitDao = sqlSession.getMapper(FruitMapper.class);
        int result = fruitDao.addFruit(fruit);
        sqlSession.commit();
        sqlSession.close();
        return result;
    }
    //条件查询
    public int getFruitId(int fruitId){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        FruitMapper fruitMapper = sqlSession.getMapper(FruitMapper.class);
        int result = fruitMapper.getFruitId(fruitId);
        sqlSession.close();
        return  result;
    }
    //修改
    public int update(Fruit fruit){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        FruitMapper fruitMapper = sqlSession.getMapper(FruitMapper.class);
        int result = fruitMapper.update(fruit);

        sqlSession.commit();
        sqlSession.close();
        return result;
    }
    //删除
    public int delete(int id){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        int count = sqlSession.getMapper(FruitMapper.class).delFruit(id);
        sqlSession.commit();
        return count;
    }
}
